﻿using System;
using System.Windows.Forms;
using System.Drawing;

namespace QuickShot.Forms.Controls
{
    /// <summary>
    /// <auhtor> Mava! </auhtor>
    /// </summary>
    public sealed class TransitionTabControl : TabControl
    {
        public bool Animated { get; set; }
        private int oldIndex = 0;

        public TransitionTabControl()
        {
           SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
        }

        public void Initialize()
        {
            this.Animated = false;

            for (int i = 1; i <= TabPages.Count; i++)
            {
                SelectedIndex = i;
            }

            SelectedIndex = 0;
            Animated = true;
        }

        protected override void OnDeselected(TabControlEventArgs e)
        {
            oldIndex = e.TabPageIndex;
        }

        protected override void OnSelected(TabControlEventArgs e)
        {
            if (Animated)
            {
                if (oldIndex < e.TabPageIndex)
                    DoAnimationScrollRight(TabPages[oldIndex], TabPages[e.TabPageIndex]);
                else
                    DoAnimationScrollLeft(TabPages[oldIndex], TabPages[e.TabPageIndex]);
            }
        }

        private void DoAnimationScrollLeft(Control firstControl, Control secondControl)
        {
            Graphics ControlGraphics = firstControl.CreateGraphics();
            Bitmap FirstControlBitmap = new Bitmap(firstControl.Width, firstControl.Height);
            Bitmap SecondControlBitmap = new Bitmap(secondControl.Width, secondControl.Height);

            firstControl.DrawToBitmap(FirstControlBitmap, new Rectangle(0, 0, firstControl.Width, firstControl.Height));
            secondControl.DrawToBitmap(SecondControlBitmap, new Rectangle(0, 0, secondControl.Width, secondControl.Height));

            foreach (Control c in firstControl.Controls)
                c.Hide();

            int slide = firstControl.Width - (firstControl.Width % 5);
            int i;

            for (i = 0; i <= slide; i += 5)
            {
                ControlGraphics.DrawImage(FirstControlBitmap, new Rectangle(i, 0, firstControl.Width, firstControl.Height));
                ControlGraphics.DrawImage(SecondControlBitmap, new Rectangle(i - secondControl.Width, 0, secondControl.Width, secondControl.Height));
            }

            i = firstControl.Width;
            ControlGraphics.DrawImage(FirstControlBitmap, new Rectangle(i, 0, firstControl.Width, firstControl.Height));
            ControlGraphics.DrawImage(SecondControlBitmap, new Rectangle(i - secondControl.Width, 0, secondControl.Width, secondControl.Height));

            SelectedTab = (TabPage)secondControl;

            foreach (Control c in firstControl.Controls)
                c.Show();
        }

        private void DoAnimationScrollRight(Control FirstControl, Control SecondControl)
        {
            Graphics ControlGraphics = FirstControl.CreateGraphics();
            Bitmap FirstControlBitmap = new Bitmap(FirstControl.Width, FirstControl.Height);
            Bitmap SecondControlBitmap = new Bitmap(SecondControl.Width, SecondControl.Height);

            FirstControl.DrawToBitmap(FirstControlBitmap, new Rectangle(0, 0, FirstControl.Width, FirstControl.Height));
            SecondControl.DrawToBitmap(SecondControlBitmap, new Rectangle(0, 0, SecondControl.Width, SecondControl.Height));

            foreach (Control c in FirstControl.Controls)
                c.Hide();

            int slide = FirstControl.Width - (FirstControl.Width % 5);
            int i;

            for (i = 0; i >= -slide; i -= 5) // += -5
            {
                ControlGraphics.DrawImage(FirstControlBitmap, new Rectangle(i, 0, FirstControl.Width, FirstControl.Height));
                ControlGraphics.DrawImage(SecondControlBitmap, new Rectangle(i + SecondControl.Width, 0, SecondControl.Width, SecondControl.Height));
            }

            i = FirstControl.Width;
            ControlGraphics.DrawImage(FirstControlBitmap, new Rectangle(i, 0, FirstControl.Width, FirstControl.Height));
            ControlGraphics.DrawImage(SecondControlBitmap, new Rectangle(i + SecondControl.Width, 0, SecondControl.Width, SecondControl.Height));
            this.SelectedTab = (TabPage)SecondControl;

            foreach (Control c in FirstControl.Controls)
                c.Show();
        }
    }
}
